import { Controller, Get, Req, Res } from "routing-controllers";
import { Service, Inject } from "typedi";
import { LOGIN_INTERFACE, loginInterface } from "service/login/login";
import { VALID_TOKEN, PERMISSIONS, RES_INFO } from "config/const";

@Controller()
@Service()
export class LoginController {
  @Inject(LOGIN_INTERFACE) private readonly loginService: loginInterface;

  // 登录跳转
  @Get("/oauth2/login")
  login(@Req() request: any, @Res() response: any) {
    response.redirect(
      this.loginService.getRedirectUrl({ request, response }).url
    );
    return response;
  }

  // 登录成功回调
  @Get("/oauth2/callback")
  async callback(@Req() request: any, @Res() response: any): Promise<any> {
    return this.loginService.afterCallBack({ request, response });
  }

  // 获取用户信息
  @Get("/oauth2/get_user_info")
  async get_user_info(@Req() request: any, @Res() response: any) {
    const userInfo = await VALID_TOKEN({ request, response });
    return this.loginService.getUserInfo({ userInfo, request, response });
  }

  // 获取权限
  @Get("/permissions")
  async permissions() {
    return RES_INFO({ data: PERMISSIONS });
  }

  // 获取所有人员
  @Get("/oauth2/acc_structure")
  async acc_structure(@Req() request: any, @Res() response: any): Promise<any> {
    return this.loginService.accStructure({ request, response });
  }
}
